<html>
<head><meta charset="utf-8"><title>const-generics · project-safe-transmute · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/index.html">project-safe-transmute</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html">const-generics</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="198793972"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/198793972" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#198793972">(May 26 2020 at 17:21)</a>:</h4>
<p>With a recent PR implementing lazy normalization, const-generics can now be used to conditionally implement traits based on a type's layout — no type level programming necessary! Here's an <code>Unaligned</code> trait implemented in like 14 lines of code (excluding whitespace):</p>
<p><a href="https://play.rust-lang.org/?version=nightly&amp;edition=2018&amp;gist=1998e7fcbaf996e644f41f1d6ed5e462">https://play.rust-lang.org/?version=nightly&amp;edition=2018&amp;gist=1998e7fcbaf996e644f41f1d6ed5e462</a></p>



<a name="198794181"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/198794181" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#198794181">(May 26 2020 at 17:23)</a>:</h4>
<p>Oh wow! That's awesome! Let me know if you want help experimenting with implementing Typic using this. I imagine we'll still run into many issues...</p>



<a name="198794518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/198794518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#198794518">(May 26 2020 at 17:26)</a>:</h4>
<p>Yeah, I have no idea whether this level of expressivity is even intended...</p>



<a name="199172512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199172512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199172512">(May 29 2020 at 15:46)</a>:</h4>
<p>this is on the same level of readability as C++ template programming... :/</p>



<a name="199172556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199172556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199172556">(May 29 2020 at 15:46)</a>:</h4>
<p>I hope we can avoid Rust becoming as unreadable as the C++ STL types</p>



<a name="199183700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199183700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199183700">(May 29 2020 at 17:12)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> It's nowhere near as bad as <a href="https://github.com/jswrenn/typic/blob/6c77444b8732d6a6bd7f8f85538085add98c02ba/typic/src/private/layout/into_bytelevel/field.rs#L16-L43">*actual* type-level programming</a>, IMO, which has viral awfulness.</p>
<p>That trick of implementing a trait on things matching a <code>const fn</code> predicate has a constant seven line cost—regardless of how complicated the predicate is. The only public facing aspect of it, the <code>Unaligned</code> trait, doesn't require any knowledge of advanced type system stuff to use, too.</p>
<p>From a notational perspective, it'd be great to _not_ have to create that intermediate <code>IsUnaligned</code> trait to check for equality. The 'distilled' version of that example might look something like this:</p>
<div class="codehilite"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">trait</span><span class="w"> </span><span class="n">Unaligned</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="w"> </span><span class="n">Unaligned</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">T</span><span class="w"></span>
<span class="k">where</span><span class="w"> </span><span class="n">T</span>: <span class="p">{</span><span class="n">align_of</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">1</span><span class="p">}</span><span class="w"></span>
<span class="p">{}</span><span class="w"></span>
</code></pre></div>



<a name="199186211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199186211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199186211">(May 29 2020 at 17:35)</a>:</h4>
<blockquote>
<p>That trick of implementing a trait on things matching a const fn predicate has a constant seven line cost—regardless of how complicated the predicate is. </p>
</blockquote>
<p>Hm, could this be abstracted somehow (worst-case with a macro)?</p>



<a name="199200787"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199200787" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199200787">(May 29 2020 at 19:29)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> This feels like an improvement: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=70f5106f33879e945b010a08e0be9606">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=70f5106f33879e945b010a08e0be9606</a></p>



<a name="199209490"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199209490" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199209490">(May 29 2020 at 20:42)</a>:</h4>
<p>A sketch of what implementing <code>TransmuteFrom</code> might look like with const generics: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=436731b0cb6bb6d52febe2c1cd6b78cf">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=436731b0cb6bb6d52febe2c1cd6b78cf</a></p>
<p>Unfortunately, the layout computation would still need to be written with trait logic, since <code>const fn</code>s can't allocate.</p>



<a name="199211418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199211418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199211418">(May 29 2020 at 21:00)</a>:</h4>
<p>small point of order: Unaligned should be an unsafe trait</p>



<a name="199211740"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199211740" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199211740">(May 29 2020 at 21:03)</a>:</h4>
<p>(Also TransmuteFrom and so on)</p>



<a name="199211871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199211871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199211871">(May 29 2020 at 21:04)</a>:</h4>
<p>In general this seems like a shockingly clean way to do things, but as always with generics I'm worried about clean build time and incrimental build time</p>



<a name="199219509"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199219509" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199219509">(May 29 2020 at 22:20)</a>:</h4>
<p>From the perspective of deduplicating computation, it would be a big improvement over typic! In order to produce <code>align_of::&lt;T&gt;()</code> in a const context, <code>rustc</code> has already (I presume) computed the layout of <code>T</code>. If rust exposed a little more of that machinery (e.g., a <code>const fn layout_of&lt;T&gt;()</code> intrinsic), then typic wouldn't need to maintain and run its own parallel implementation of the layout algorithm.</p>



<a name="199244190"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199244190" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199244190">(May 30 2020 at 07:42)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219211">Jack Wrenn</span> <a href="#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199200787">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> This feels like an improvement: <a href="https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=70f5106f33879e945b010a08e0be9606">https://play.rust-lang.org/?version=nightly&amp;mode=debug&amp;edition=2018&amp;gist=70f5106f33879e945b010a08e0be9606</a></p>
</blockquote>
<p><code>Predicate: Satisfied</code> has a nice ring to it, yeah :D</p>



<a name="199259832"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199259832" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199259832">(May 30 2020 at 14:43)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219211">Jack Wrenn</span> <a href="#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199209490">said</a>:</p>
<blockquote>
<p>Unfortunately, the layout computation would still need to be written with trait logic, since <code>const fn</code>s can't allocate.</p>
</blockquote>
<p>This doesn't seem _necessary_ to me. In the case of sized struct the representation of it's <code>Validity</code> slice has a statically known length and independent of the content, as given by its size and the validity representation of its fields. Similar the representation of an enum aggregates the representations of its variants. So wouldn't mutation in const-fn suffice to implement this in an algorithm, by using two levels of const-evaluation as has been a common practice in C++ already (for example to parse json at compile time into an _array_ <a href="https://github.com/CppCon/CppCon2017/raw/master/Presentations/constexpr%20ALL%20the%20things/constexpr%20ALL%20the%20things%20-%20Jason%20Turner%20and%20Ben%20Deane%20-%20CppCon%202017.pdf">slides</a>)?</p>



<a name="199262844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/const-generics/near/199262844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/const-generics.html#199262844">(May 30 2020 at 15:48)</a>:</h4>
<p>I suppose you're right! But Typic uses an optimized representation for repeating sequences of byte validities (namely, for arrays). So, it's not quite as simple as <code>[Validity; size_of::&lt;T&gt;()]</code>.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>